<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Canvas extends CI_Controller 
{

   private $data;

   public function __construct() 
   {
      parent::__construct();

      $this->data = array();
   }

   public function index() 
   {
      $this->load->helper('url');
      
      $data = array();
      
      $data['page_title'] = 'Main Otsu Code';
      
      $this->load->view('canvas/index', $data);
   }
   
   
   public function refactor() 
   {
      $this->load->helper('url');
      
      $data = array();
      
      $data['page_title'] = 'Refactored GH Class';
      $data['scripts'] = array(
          'gh.class'
      );
      
      $this->load->view('canvas/refactor', $data);
   }
   
   
   private function validateTableName($pName)
   {
      switch($pName)
      {
         case 'red':
            return 'histogram_red_23';
            
         case 'green':
            return 'histogram_green_23';
            
         case 'blue':
            return 'histogram_blue_23';
      }
      
      throw new Exception('Invalid RGB color channel specified.');
   }
   
   private function serializeData(array $pData)
   {
      $data = array();
      
      for($i = 0; $i < count($pData); $i++)
         $data['bucket_'. ($i + 1)] = $pData[$i];
      
      return $data;
   }
   
   public function testXHRPost()
   {
      $this->load->model('Testdb');
      
      // Format histogram data to fit our needs. Array keys must be converted
      // from zero-based integer values to 'bucket_X', where X is the bucket
      // number, starting from 1.
      $data = $this->serializeData($this->input->post('data'));
      
      
      // Extract and validate table name from post data
      try
      {
         $data['table_name'] = $this->validateTableName($this->input->post('channel'));
      }
      catch(Exception $e)
      {
         echo $e->getMessage();
         return false;
      }

      // Extract name of image being indexed
      $data['img'] = $this->input->post('img');

      if($this->Testdb->saveHisto($data))
         echo json_encode(array(
             'success' => true
         ));
      else
         echo json_encode(array(
             'success' => false
         ));

   }
   
   
   function matchit()
   {
      $this->load->model('Testdb');
      
      $res = $this->Testdb->matchit(array(
          'red'   => $this->serializeData($this->input->post('red_data')),
          'green' => $this->serializeData($this->input->post('green_data')),
          'blue'  => $this->serializeData($this->input->post('blue_data')),
          'img'   => $this->input->post('img')
      ));
      
      echo json_encode(array(
          'success'    => true,
          'resultHTML' => $this->Testdb->formatResultsHTML($res)
      ));
   }
   
   
   public function testdb()
   {
      $this->load->model('Testdb');
      
      
      $data = array(
  'bucket_1' => '0',
  'bucket_2' => '0',
  'bucket_3' => '0',
  'bucket_4' => '0',
  'bucket_5' => '0',
  'bucket_6' => '0',
  'bucket_7' => '0',
  'bucket_8' => '0',
  'bucket_9' => '0',
  'bucket_10' => '0',
  'bucket_11' => '0',
  'bucket_12' => '49',
  'bucket_13' => '51',
  'bucket_14' => '0',
  'bucket_15' => '0',
  'bucket_16' => '0',
  'bucket_17' => '0',
  'bucket_18' => '0',
  'bucket_19' => '0',
  'bucket_20' => '0',
  'bucket_21' => '0',
  'bucket_22' => '0',
  'bucket_23' => '0',
  'channel' => 'histogram_red_23',
  'img' => 'http://localhost/cs499/public/img/pic10.jpg');
      
      var_dump($data);
      
      echo $this->Testdb->fakeInsert($data);
      
      
      exit;
   }
   
   
   public function qunit()
   {
      $this->load->helper('url');
      
      $data = array();
      
      $data['page_title'] = 'Unit Tests for GH Class';
      $data['scripts'] = array(
          'gh.class'
      );
      
      $this->load->view('unit-test/qunit', $data);
   }
   
   
   /**
    * Generates a data set from a made up histogram (working backwards).
    * This data is used to test functions that need random data and a known
    * histogram that mathces the data.
    * 
    * @param int $pMin the lowest value to be present in the dataset
    * @param int $pMax the highest value to be present in the dataset
    * @param int $pBuckets how many buckets to divide the histogram into
    */
   public function generateTestHisto($pMin = 0, $pMax = 255, $pBuckets = 23)
   {
      echo 'Generating test histograms';
      
      $min = $pMin;
      $max = $pMax;
      $buckets = $pBuckets;
      $bucketWidth = (int)($pMax / $buckets);
      $histoLen = 0;
      echo '<p>Min = ', $min, '<br/>Max = ', $max, '</p>';
      
      
      //
      // Expected histogram
      //
      echo '<p>Expected = [';
      $resHisto = array();
      for($i = 0; $i < $buckets; $i++)
      {
         $resHisto[$i] = rand(23, 42);
         $histoLen += $resHisto[$i];
         
         echo $resHisto[$i];
         
         if($i < $buckets - 1)
            echo ', ';
      }
      echo ']</p>';
      
      echo '<hr/>';
      
      
      //
      // Dataset that produces expected
      //
      echo '<p>Histo Lenth = ', $histoLen, '</p>';
      echo '<p>Histo = [';
      
      for($i = 0; $i < $buckets; $i++)
      
         for($j = 0; $j < $resHisto[$i]; $j++)
         {
            $from =  $i * $bucketWidth;
            $to = ($i + 1) * $bucketWidth - 1;
            echo rand($from, $to), ', ';
         }
         
      echo '</p>';
      
      
      exit;
   }
   
}

/* End of file canvas.php */
/* Location: ./application/controllers/canvas.php */